package com.gin.no_gay_alliance.activity;

import static com.gin.no_gay_alliance.enums.MessageWhat.HIDE_BUTTON;
import static com.gin.no_gay_alliance.enums.MessageWhat.LOCK_VIEW_PAGER;
import static com.gin.no_gay_alliance.enums.MessageWhat.ON_SUCCESS_TOPIC_READ_REQUEST;
import static com.gin.no_gay_alliance.enums.MessageWhat.SHOW_BUTTON;
import static com.gin.no_gay_alliance.enums.MessageWhat.START_ACTIVITY_POST_ACTIVITY;
import static com.gin.no_gay_alliance.enums.MessageWhat.TOGGLE_BUTTON_VISIBILITY;
import static com.gin.no_gay_alliance.enums.MessageWhat.UNLOCK_VIEW_PAGER;
import static com.gin.no_gay_alliance.param.PreparePostParam.Action.REPLY;

import android.animation.LayoutTransition;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;

import androidx.activity.result.ActivityResult;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.gin.jackson.utils.ObjectUtils;
import com.gin.nga.callback.AbstractRedirectCallback;
import com.gin.nga.client.NgaClient;
import com.gin.nga.response.ReplyLocation;
import com.gin.nga.response.field.CustomLevel;
import com.gin.nga.utils.AnonymousUtils;
import com.gin.no_gay_alliance.R;
import com.gin.no_gay_alliance.adpter.base.BaseBindingRecyclerViewAdapter;
import com.gin.no_gay_alliance.annotation.Visibility;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.customview.PagerButton;
import com.gin.no_gay_alliance.database.entity.ReplyHistoryTopic;
import com.gin.no_gay_alliance.database.entity.ScanTask;
import com.gin.no_gay_alliance.database.entity.TopicHistory;
import com.gin.no_gay_alliance.database.manager.ReplyHistoryManager;
import com.gin.no_gay_alliance.databinding.ActivityTopicReadBinding;
import com.gin.no_gay_alliance.databinding.DialogRecyclerViewGridBinding;
import com.gin.no_gay_alliance.databinding.DialogScanTaskBinding;
import com.gin.no_gay_alliance.databinding.ItemLabelValueBinding;
import com.gin.no_gay_alliance.exception.RequireParamException;
import com.gin.no_gay_alliance.fragment.reply.ReplyPageFragment;
import com.gin.no_gay_alliance.param.PreparePostParam;
import com.gin.no_gay_alliance.param.TopicReadParam;
import com.gin.no_gay_alliance.utils.AlertDialogUtils;
import com.gin.no_gay_alliance.utils.CopyUtils;
import com.gin.no_gay_alliance.utils.InputMethodManagerUtils;
import com.gin.no_gay_alliance.utils.ViewUtils;
import com.gin.no_gay_alliance.viewmodel.PostResult;
import com.gin.no_gay_alliance.viewmodel.ReadItem;
import com.gin.no_gay_alliance.viewmodel.ScrollTarget;
import com.gin.no_gay_alliance.viewmodel.TopicItem;

import java.io.IOException;
import java.util.Locale;

import okhttp3.Call;

/**
 * 阅读主题活动 ViewBinding模式
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/26 09:37
 **/
public class TopicReadActivity extends BaseBindingActivity<ActivityTopicReadBinding> implements PagerButton.OnPageChangedListener {
    public static final String TAG = TopicReadActivity.class.getSimpleName();
    /**
     * 参数
     */
    TopicReadParam param;
    /**
     * 最大页数
     */
    int totalPage = 1;
    /**
     * 当前返回的响应对象
     */
    ReadItem readItem;
    private ScrollTarget scrollTarget;

    /**
     * 清除滚动目标，暴露给分页使用
     */
    public void clearScrollTarget() {
        if (scrollTarget != null) {
            Log.d(TAG, "滚动目标: 消耗");
            this.scrollTarget = null;
        }
    }


    /**
     * 解析意图
     *
     * @param intent 意图
     */
    @Override
    protected void handleIntent(Intent intent) {
        // 从意图中直接获取参数
        param = intent.getParcelableExtra(TopicReadParam.class.getName());
        if (param == null) {
            // 尝试从url中获取参数
            String url = intent.getDataString();
            if (!ObjectUtils.isEmpty(url)) {
                param = TopicReadParam.parseUrl(url);
            }
        }
        if (param == null) {
            throw new RequireParamException();
        }

        // 尝试从参数中获取当前最大页数(临时)
        setTotalPage(param.totalPage);
        setTotalPage(param.page);
    }

    @Override
    protected void onCreate() {
        b.getRoot().getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
        // 隐藏按钮
        setButtonsVisibility(View.INVISIBLE);
        initViewPager2();
        // 配置翻页器
        b.btnPager.init(this, handler, this);
        // 配置发帖按钮
        b.btnPost.setOnClickListener(this);
        b.btnRefresh.setOnClickListener(this);
    }

    /**
     * 装载右上角菜单
     *
     * @param menu         菜单
     * @param menuInflater 菜单渲染器
     */
    @Override
    protected void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
        menuInflater.inflate(R.menu.base_option_menu, menu);
        // 单回复模式下增加跳转到主题按钮
        if (param.topicId == null) {
            menuInflater.inflate(R.menu.single_reply_menu, menu);
        }
        // 在只看TA模式下不加载
        if (param.authorUid == null) {
            menuInflater.inflate(R.menu.share_menu, menu);
            menuInflater.inflate(R.menu.topic_read_menu, menu);
        }
    }

    /**
     * 处理启动器启动的活动返回的结果
     *
     * @param activityResult 结果
     */
    @Override
    public void onActivityResult(ActivityResult activityResult) {
        Intent intent = activityResult.getData();
        if (intent == null) {
            return;
        }
        PostResult result = intent.getParcelableExtra(PostResult.class.getName());
        //处理结果
        if (result != null) {
            if (param.authorUid != null) {
                // 如果处于只看TA模式，直接翻到最后一页
                ui(() -> b.vp2.setCurrentItem(totalPage, false));
                return;
            }
            // 获取该回复所在页数

            // 如果返回定位中有回复id，查询其所在页数再跳转
            if (result.replyId != null) {
                MyApplication.getClient().toReply(result.replyId, new AbstractRedirectCallback() {
                    @Override
                    protected void onRedirect(String location) {

                        // 翻页
                        ReplyLocation loc = new ReplyLocation(location);
                        // 目标页数
                        int index = loc.getPage() - 1;
                        totalPage = Math.max(totalPage, index + 1);
                        if (b.vp2.getCurrentItem() == index) {
                            //如果目标页 就是当前页,刷新
                            refresh();
                        } else {
                            // 设置滚动目标
                            Log.d(TAG, String.format(Locale.CHINA, "设置滚动目标 回复Id: %d , 位于第 %d 页", loc.getReplyId(), loc.getPage()));
                            scrollTarget = new ScrollTarget(loc.getPage(), loc.getReplyId());
                            // 不是当前页, 翻页到指定页
                            ui(() -> b.vp2.setCurrentItem(index, false));
                        }
                    }

                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                        e.printStackTrace();
                    }
                });
            } else {
                // 没有回复id，表示为编辑主楼操作,刷新
                refresh();
            }
        }
    }

    /**
     * 处理消息
     *
     * @param message 消息
     * @return 是否消耗该消息
     */
    @Override
    public boolean handleMessage(@NonNull Message message) {
        // 获取到请求响应
        if (message.what == ON_SUCCESS_TOPIC_READ_REQUEST.ordinal()) {
            // 显示按钮
            setButtonsVisibility(View.VISIBLE);
            // 更新响应数据
            readItem = (ReadItem) message.obj;
            // 更新最大页数
            setTotalPage(readItem.totalPage);
            // 分页数据同步到翻页器
            b.btnPager.setData(readItem.page, totalPage);
            // 更新toolbar标题
            setToolbarTitle(readItem.title);

            // 浏览历史
            if (readItem.topicItem != null && readItem.topicItem.authorId != null) {
                TopicHistory topicHistory = new TopicHistory(readItem.topicItem);
                //当前页
                topicHistory.page = readItem.page;
                MyApplication.app.ngaDatabase.topicHistoryDao().add(topicHistory);
            }

            return true;
        }
        // 切换右下角按钮的显示状态
        if (message.what == TOGGLE_BUTTON_VISIBILITY.ordinal()) {
            toggleButtonsVisibility();
        }
        // 显示右下角按钮
        if (message.what == SHOW_BUTTON.ordinal()) {
            setButtonsVisibility(View.VISIBLE);
        }
        // 隐藏右下角按钮
        if (message.what == HIDE_BUTTON.ordinal()) {
            setButtonsVisibility(View.GONE);
        }
        // 启动发帖活动
        if (message.what == START_ACTIVITY_POST_ACTIVITY.ordinal()) {
            launcher.launch(((PreparePostParam) message.obj).buildIntent(this));
        }
        // 锁定 viewPager2 禁止翻页
        if (message.what == LOCK_VIEW_PAGER.ordinal()) {
            b.vp2.setUserInputEnabled(false);
            return true;
        }
        // 解锁 viewPager2
        if (message.what == UNLOCK_VIEW_PAGER.ordinal()) {
            b.vp2.setUserInputEnabled(true);
            return true;
        }

        return super.handleMessage(message);
    }

    /**
     * 如果布局中直接有toolbar, 在此提供, 如果使用Fragment布局中的toolbar,Fragment渲染完成后调用 setToolbar 方法装载
     *
     * @return toolbar
     */
    @Override
    protected Toolbar initToolbar() {
        return b.toolbar;
    }

    /**
     * 点击事件统一配置
     *
     * @param v 被点击的view
     */
    @Override
    public void onClick(View v) {
        // 发帖按钮
        if (v == b.btnPost && param != null) {
            launcher.launch(new PreparePostParam(REPLY, param.topicId, 0L).buildIntent(this));
        }
        if (v == b.btnRefresh) {
            refresh();
        }
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem item = menu.findItem(R.id.menu_item_only_author);
        if (item != null) {
            // 当楼主为匿名用户时,只看TA功能不可用
            item.setVisible(getAuthorUid() > 0);
        }

        // 根据主题是否被观察，决定显示项目
        if (readItem != null) {
            boolean observed = ReplyHistoryManager.isObservedTopic(readItem.topicId);
            MenuItem add = menu.findItem(R.id.menu_item_add_observed_topic);
            if (add != null) {
                add.setVisible(!observed);
            }
            MenuItem remove = menu.findItem(R.id.menu_item_remove_observed_topic);
            if (remove != null) {
                remove.setVisible(observed);
            }
        }

        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        // 添加扫描
        if (itemId == R.id.menu_item_add_scan_task) {
            DialogScanTaskBinding b = MyApplication.app.managerFactory.getViewBindingManager().getDialogView(DialogScanTaskBinding.class, this);
            b.etTitle.setText(readItem.title);
            b.etPageStart.setText("1");

            AlertDialog dialog = AlertDialogUtils.showConfirm(this, "添加扫描任务", null, b.getRoot(), (d, which) -> {
                if (which == DialogInterface.BUTTON_POSITIVE) {
                    ScanTask scanTask = new ScanTask();
                    scanTask.topicId = readItem.topicId;
                    scanTask.totalPage = readItem.totalPage;
                    scanTask.status = ScanTask.Status.WAITING;
                    scanTask.title = b.etTitle.getText().toString();
                    scanTask.pageStart = Integer.parseInt(b.etPageStart.getText().toString());
                    scanTask.watching = b.swWatching.isChecked();


                    if (!ObjectUtils.isEmpty(b.etPageEnd.getText().toString())) {
                        scanTask.pageEnd = Integer.valueOf(b.etPageEnd.getText().toString());
                    }

                    MyApplication.app.ngaDatabase.scanTaskDao().add(scanTask);
                    toast("添加成功");
                }
            });

            // 确定按钮
            Button button = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
            // 参数校验
            TextWatcher watcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (ObjectUtils.isEmpty(b.etTitle.getText().toString())) {
                        button.setEnabled(false);
                    } else button.setEnabled(!ObjectUtils.isEmpty(b.etPageStart.getText().toString()));
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            };
            b.etTitle.addTextChangedListener(watcher);
            b.etPageStart.addTextChangedListener(watcher);
            b.etPageEnd.addTextChangedListener(watcher);
        }
        // 添加观察
        if (itemId == R.id.menu_item_add_observed_topic && readItem != null) {
            AlertDialogUtils.showConfirm(this, "添加观察主题", getString(R.string.tips_add_observed_topic), (d, which) -> {
                if (which == DialogInterface.BUTTON_POSITIVE) {
                    ReplyHistoryManager.addObservedTopic(new ReplyHistoryTopic(readItem.topicId, readItem.title));
                    toast("添加成功");
                    // 开启观察时清空所有缓存
                    MyApplication.app.managerFactory.getTopicReadManager().clear();
                    refresh();
                }
            });
        }
        // 移除观察
        if (itemId == R.id.menu_item_remove_observed_topic && readItem != null) {
            AlertDialogUtils.showConfirm(this, "移除观察主题", getString(R.string.tips_remove_observed_topic), (d, which) -> {
                if (which == DialogInterface.BUTTON_POSITIVE) {
                    ReplyHistoryManager.removeObservedTopic(readItem.topicId);
                    toast("移除成功");
                }
            });
        }

        // 只看我
        if (itemId == R.id.menu_item_only_me) {
            NgaClient client = MyApplication.getClient();
            TopicReadActivity.startActivity(this, param.topicId, client.getUserId(), client.getUsername());
            return true;
        }
        // 只看楼主
        if (itemId == R.id.menu_item_only_author && getAuthorUid() > 0) {
            TopicReadActivity.startActivity(this, param.topicId, getAuthorUid(), "楼主");
            return true;
        }
        // 复制链接
        if (itemId == R.id.menu_item_copy_link) {
            CopyUtils.copyLink(this, param.topicId, null);
            return true;
        }
        // 复制bbsCode
        if (itemId == R.id.menu_item_copy_bbs_code && readItem != null) {
            CopyUtils.copyBbsCode(this, param.topicId, null, readItem.title, null);
            return true;
        }
        // 提醒
        if (itemId == R.id.menu_item_noti) {
            NoticeActivity.startActivity(this);
            return true;
        }
        // 私信
        if (itemId == R.id.menu_item_pm) {
            PriMsgActivity.startActivity(this);
            return true;
        }
        // 历史
        if (item.getItemId() == R.id.menu_item_topic_history) {
            TopicHistoryActivity.startActivity(this);
            return true;
        }
        // 举报记录
        if (item.getItemId() == R.id.menu_item_reporting_record) {
            ReportingRecordActivity.startActivity(this);
            return true;
        }
        // 添加收藏
        if (itemId == R.id.menu_item_add_favor) {
            MyApplication.app.managerFactory.getFavorManager().topicAddDialog(this, handler, param.topicId != null ? param.topicId : readItem.topicId, 0, data -> toast(data.getMessage()));
            return true;
        }
        // 打开收藏夹
        if (itemId == R.id.menu_item_favor_folder) {
            TopicListActivity.startActivityFavor(this);
        }
        // 声望等级
        if (itemId == R.id.menu_item_custom_level) {
            DialogRecyclerViewGridBinding gridBinding = DialogRecyclerViewGridBinding.inflate(getLayoutInflater());
            gridBinding.recyclerView.setAdapter(new BaseBindingRecyclerViewAdapter<ItemLabelValueBinding, CustomLevel>(this, readItem.customLevels) {
                @Override
                protected void onBindViewHolder(ViewHolder<ItemLabelValueBinding> holder,
                                                @NonNull ItemLabelValueBinding b, int position, CustomLevel item) {
                    b.tvLabel.setText(item.getName());
                    b.tvValue.setText(String.valueOf(item.getRank()));
                }
            });

            AlertDialogUtils.showConfirm(this, "声望等级", null, gridBinding.getRoot(), null);

            return true;
        }
        //  跳转到主题
        if (itemId == R.id.menu_item_topic) {
            MyApplication.getClient().toReply(param.replyId, new AbstractRedirectCallback() {
                @Override
                protected void onRedirect(String location) {
                    startActivity(TopicReadActivity.this, new ReplyLocation(location));
                }

                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    e.printStackTrace();
                }
            });
            return true;
        }
        //  楼层跳转
        if (itemId == R.id.menu_item_jump_to_level) {
            EditText et = new EditText(this);
            et.setHint("1 ~ " + readItem.total);
            et.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (ObjectUtils.isEmpty(s)) {
                        return;
                    }
                    int level = Integer.parseInt(s.toString());
                    if (level > readItem.total) {
                        et.setText(String.valueOf(readItem.total));
                        et.setSelection(0, et.getText().length());
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
            et.setInputType(EditorInfo.TYPE_CLASS_NUMBER);

            AlertDialogUtils.showConfirm(this, getString(R.string.jump_to_level), null, et, (dialog, which) -> {
                if (which == DialogInterface.BUTTON_POSITIVE) {
                    int level = Integer.parseInt(et.getText().toString());
                    int page = level / 20 + 1;
                    Log.d(TAG, String.format(Locale.CHINA, "设置滚动目标 楼层号: %d , 位于第 %d 页", level, page));
                    // 设置滚动目标
                    scrollTarget = new ScrollTarget(level);
                    // 翻页
                    b.vp2.setCurrentItem(page - 1, false);
                }
            });

            et.requestFocus();
            InputMethodManagerUtils.show(handler, et);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 翻页器执行翻页操作时的回调
     *
     * @param page 目标页数
     */
    @Override
    public void onPageChanged(int page) {
        b.vp2.setCurrentItem(page - 1, false);
    }

    /**
     * 初始化ViewPager2
     */
    private void initViewPager2() {
        //初始化ViewPager2
        b.vp2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                TopicReadParam p = new TopicReadParam();

                if (param.backupId != null) {
                    // 指定了备份id，为编辑历史模式
                    p.topicId = param.topicId;
                    p.replyId = param.replyId;
                    p.backupId = param.backupId;
                } else if (param.replyId != null && (param.topicId == null || param.page == null)) {
                    // 如果未指定主题id, 则为单回复模式
                    p.replyId = param.replyId;
                } else {
                    // 如果指定了主题id，则回复id为滚动目标
                    p.topicId = param.topicId;
                    p.authorUid = param.authorUid;
                    p.page = position + 1;
                    if (param.replyId != null) {
                        Log.d(TAG, String.format(Locale.CHINA, "设置滚动目标 回复Id: %d , 位于第 %d 页", param.replyId, param.page));
                        scrollTarget = new ScrollTarget(param.page, param.replyId);
                    }
                }
                return ReplyPageFragment.newInstance(p);
            }

            @Override
            public int getItemCount() {
                return totalPage;
            }
        });

        // ViewPager2翻页时, 分页数据同步到翻页器
        b.vp2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                int page = position + 1;
                Log.d(TAG, "当前页数: " + page);
                b.btnPager.setData(page, totalPage);
            }
        });

        // 如果参数指定了页数，翻到该页
        if (param.page != null) {
            Log.d(TAG, "翻页到: " + param.page);
            b.vp2.setCurrentItem(param.page - 1, false);
            b.btnPager.setData(param.page, totalPage);
        }
    }

    /**
     * 刷新当前页
     */
    private void refresh() {
        Fragment fragment = getCurrentFragment(b.vp2.getCurrentItem());
        if (fragment instanceof ReplyPageFragment) {
            ((ReplyPageFragment) fragment).refresh();
        }
    }

    /**
     * 切换右下角按钮的可见性
     */
    private void toggleButtonsVisibility() {
        if (b.btnPager.getVisibility() == View.VISIBLE) {
            setButtonsVisibility(View.GONE);
        } else {
            setButtonsVisibility(View.VISIBLE);
        }
    }

    /**
     * 启动“只看TA”活动
     *
     * @param context   上下文
     * @param username  用户名
     * @param topicId   主题id
     * @param authorUid 用户id
     */
    public static void startActivity(Context context, long topicId, long authorUid, String username) {
        TopicReadParam param = new TopicReadParam();
        param.authorUid = authorUid;
        param.topicId = topicId;
        param.startActivity(context, username + " 在主题内的发言");
    }

    public static void startActivity(Context context, String title, long topicId) {
        startActivity(context, title, topicId, null, null, null);
    }

    public static void startActivity(Context context, long replyId) {
        startActivity(context, null, null, null, replyId, null);
    }

    public static void startActivity(Context context, TopicItem topicItem, int page) {
        startActivity(context, topicItem.title, topicItem.topicId, page, topicItem.replyCount + 1);
    }

    public static void startActivity(Context context, String title, long topicId, int page, int total) {
        TopicReadParam p = new TopicReadParam();
        p.topicId = topicId;
        p.page = page;
        p.total = total;
        startActivity(context, title, p);
    }

    public static void startActivity(Context context, String title, Long topicId, Integer page, Long replyId, Integer totalPage) {
        TopicReadParam p = new TopicReadParam();
        p.topicId = topicId;
        p.page = page;
        p.replyId = replyId;
        p.totalPage = totalPage;
        startActivity(context, title, p);
    }

    public static void startActivity(Context context, String title, TopicReadParam param) {
        Intent intent = new Intent(context, TopicReadActivity.class);
        intent.putExtra(BaseBindingActivity.INTENT_KEY_TITLE, title);
        intent.putExtra(TopicReadParam.class.getName(), param);
        context.startActivity(intent);
    }

    public static void startActivity(Context context, ReplyLocation loc) {
        startActivity(context, null, loc.getTopicId(), loc.getPage(), loc.getReplyId(), loc.getPage());
    }

    /**
     * 获取楼主的用户id
     *
     * @return 楼主的用户id
     */
    private long getAuthorUid() {
        if (readItem == null || readItem.authorUid == null) {
            return -1;
        }
        return AnonymousUtils.isAnonymous(readItem.authorUid) ? -1 : Long.parseLong(readItem.authorUid);
    }

    /**
     * 获取滚动目标, 暴露给分页使用
     *
     * @return 滚动目标
     */
    public ScrollTarget getScrollTarget() {
        return scrollTarget;
    }

    /**
     * 设置右下角按钮的可见性
     *
     * @param visible 可见性
     */
    private void setButtonsVisibility(@Visibility int visible) {
        ViewUtils.setVisibility(visible, b.btnPager, b.btnPost, b.btnRefresh);
    }

    /**
     * 设置最大页数
     *
     * @param totalPage 最大页数
     */
    public void setTotalPage(Integer totalPage) {
        if (totalPage != null && this.totalPage != totalPage) {
            this.totalPage = Math.max(this.totalPage, totalPage);
            Log.d(TAG, "当前最大页数更新: " + this.totalPage);
        }
    }

}
